import { Router, RouteRecordRaw } from 'vue-router';
import autoLoadModuleRoutes from './module';
import userStore from '@/store/user-store';
import getRoutes from './view';
import utils from '@/utils';

// 所有路由
let routes: RouteRecordRaw[] = utils.env.VITE_ROUTER_AUTOLOAD ? getRoutes() : autoLoadModuleRoutes();

// 路由过滤注册
function autoLoad(router: Router) {
  const user = userStore();
  routes = routes.filter(route => {
    route.children = route.children?.filter(r => {
      // 如果没有设置权限(公共路由)直接方向
      const permission = r.meta?.permission;
      return permission ? user.info?.permissions.includes(permission) : true;
    });
    return route;
  });
  console.log('🚀 ~ file: index.ts ~ line 21 ~ autoLoad ~ routes', routes);
  routes.forEach(r => router.addRoute(r));
}
export default autoLoad;
